package com.yc.sole.erp.controller;

import com.yc.sole.common.domain.StateCountResp;
import com.yc.sole.erp.biz.ShipmentBiz;
import com.yc.sole.erp.request.ShipmentQueryReq;
import com.yc.sole.erp.request.ShipmentReq;
import com.yc.sole.erp.response.ShipmentOrderResp;
import com.yc.sole.erp.response.ShipmentResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 送货管理
 *
 * @author yizuomin
 * @date 2024-01-16
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ShipmentController {

    private final ShipmentBiz shipmentBiz;

    /**
     * 新增
     *
     * @param shipmentReq
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:save", description = "新增送货信息")
    @PostMapping("/api/sale/shipment/save")
    public Result<Void> saveShipment(@RequestBody @Validated(SaveGroup.class) ShipmentReq shipmentReq) {
        shipmentBiz.saveShipment(shipmentReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param shipmentReq
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:update", description = "修改送货信息")
    @PostMapping("/api/sale/shipment/update")
    public Result<Void> updateShipmentById(@RequestBody @Validated(UpdateGroup.class) ShipmentReq shipmentReq) {
        shipmentBiz.updateShipmentById(shipmentReq);
        return Result.success();
    }

    /**
     * 通过Id查询
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:select", description = "通过Id查询")
    @PostMapping("/api/sale/shipment/{id}")
    public Result<ShipmentResp> selectShipmentById(@PathVariable Long id) {
        return Result.data(shipmentBiz.selectShipmentById(id));
    }

    /**
     * 通过订单Id查询
     *
     * @param orderId
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:select", description = "通过订单Id查询")
    @PostMapping("/api/sale/shipment/order-id/{orderId}")
    public Result<List<ShipmentOrderResp>> selectShipmentByOrderId(@PathVariable Long orderId) {
        return Result.data(shipmentBiz.selectShipmentByOrderId(orderId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:delete", description = "删除送货信息")
    @PostMapping("/api/sale/shipment/delete")
    public Result<Void> deleteShipment(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        shipmentBiz.deleteShipment(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "sale:shipment:select", description = "查询送货信息")
    @PostMapping("/api/sale/shipment/page")
    public Result<Paging<ShipmentResp>> selectShipmentPage(@RequestBody @Validated BasePageReq<ShipmentQueryReq> basePageReq) {
        return Result.data(shipmentBiz.selectShipmentPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "sale:shipment:select", description = "查询送货信息")
    @PostMapping("/api/sale/shipment/detail-page")
    public Result<Paging<ShipmentResp>> selectPageWithOrder(@RequestBody @Validated BasePageReq<ShipmentQueryReq> basePageReq) {
        return Result.data(shipmentBiz.selectPageWithOrder(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 查询送货类型的数量
     *
     * @return RestResult
     */
    @PreAuth(value = "sale:shipment:select", description = "查询送货类型的数量")
    @PostMapping("/api/sale/shipment/shipment-type-count")
    public Result<List<StateCountResp>> selectShipmentTypeCount(@RequestBody ShipmentQueryReq queryReq) {
        return Result.data(shipmentBiz.selectShipmentTypeCount(queryReq));
    }
}
